package com.geek

import logger
import org.springframework.context.annotation.*
import org.springframework.core.type.AnnotatedTypeMetadata

class WindowsCondition : Condition {
    override fun matches(context: ConditionContext, metadata: AnnotatedTypeMetadata): Boolean =
        context.environment.getProperty("os.name")?.contains("Windows") ?: false
}

class LinuxCondition : Condition {
    override fun matches(context: ConditionContext, metadata: AnnotatedTypeMetadata): Boolean =
        context.environment.getProperty("os.name")?.contains("Linux") ?: false
}

interface ListService {
    fun showListCmd(): String
}

class WindowsListService : ListService {
    override fun showListCmd(): String = "dir"
}

class LinuxListService : ListService {
    override fun showListCmd(): String = "ls"
}

@Configuration
class ConditionConfig {

    @Bean
    @Conditional(WindowsCondition::class)
    fun windowsListService(): ListService = WindowsListService()

    @Bean
    @Conditional(LinuxCondition::class)
    fun linuxListService(): ListService = LinuxListService()
}


fun main() {
    AnnotationConfigApplicationContext(ConditionConfig::class.java).use { context ->
        val listService = context.getBean(ListService::class.java)
        logger.info("${context.environment.getProperty("os.name")} 系统下的列表命令为：${listService.showListCmd()}")
    }
}
